package com.chenfan.magic.tags;

import com.chenfan.magic.utils.IDKit;
import com.chenfan.magic.utils.StringKit;
import lombok.Data;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Element;

import java.io.Serializable;

/**
 * @author lm
 * validate标签模型
 * <validate id="123456"></validate>
 */
@Data
public class TagSms extends TagAbstract implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String tagNames = "sms";

    protected String type;
    protected String serial;
    protected String sign;
    protected String mobile;
    protected String ext;

    public TagSms() {
    }

    public TagSms(Element resolveTag, int index) {
        this.element = resolveTag.toString();
        this.id = resolveTag.attr("id");
        if (StringKit.isBlank(this.id)) {
            this.id = IDKit.fastUUID() + "";
        }
        this.name = resolveTag.attr("name");
        String sort = resolveTag.attr("sort");
        if (StringKit.isBlank(sort)) {
            this.sort = index - 10000;
        } else {
            this.sort = Integer.parseInt(sort);
        }
        this.resultKey = resolveTag.attr("result-key");
        if (StringKit.isBlank(this.resultKey)) {
            this.resultKey = "result_" + this.id;
        }
        String respond = resolveTag.attr("respond");
        if (StringKit.isBlank(respond)) {
            this.respond = true;
        } else {
            this.respond = Boolean.valueOf(respond);
        }
        String resolve = resolveTag.attr("resolve");
        if (StringKit.isBlank(resolve)) {
            this.resolve = true;
        } else {
            this.resolve = Boolean.valueOf(resolve);
        }
        this.context = StringEscapeUtils.unescapeHtml4(resolveTag.text());

        this.__sort = resolveTag.attr(":sort");
        this.__respond = resolveTag.attr(":respond");
        this.__resolve = resolveTag.attr(":resolve");
        this.__resultKey = resolveTag.attr(":result-key");
        this.engine = resolveTag.attr("engine");

        this.type = resolveTag.attr("type");
        if (StringKit.isBlank(this.type)) {
            this.type = "mw";
        }
        this.serial = resolveTag.attr("serial");
        if (StringKit.isBlank(this.serial)) {
            this.serial = this.id;
        }
        this.sign = resolveTag.attr("sign");
        this.mobile = resolveTag.attr("mobile");
        this.ext = resolveTag.attr("ext");

    }
}
